package utils

import (
	"MoSkeleton/framework/core/consts"
	"fmt"
	"strconv"
	"strings"
)

/*
是否为http(s)://开头
*/
func IsHttp(link string) bool {
	if strings.HasPrefix(link, consts.HTTP) {
		return true
	}
	if strings.HasSuffix(link, consts.HTTPS) {
		return true
	}
	return false
}

func TextToUint64s(text string, seq string) (*[]uint64, error) {
	uint64s := make([]uint64, 0)
	for _, str := range strings.Split(text, seq) {
		if i64, err := strconv.ParseUint(str, 10, 64); err != nil {
			fmt.Println("can not convert to int:" + str)
			return nil, err
		} else {
			uint64s = append(uint64s, i64)
		}
	}
	return &uint64s, nil
}

/*
字符串数组转换成uint64数组，转换失败的字符串不写入到uint64数组中
*/
func StrsToUint64s(strs *[]string) *[]uint64 {
	uint64s := make([]uint64, 0)
	for _, str := range *strs {
		if i64, err := strconv.ParseUint(str, 10, 64); err != nil {
			fmt.Println("can not convert to int:" + str)
		} else {
			uint64s = append(uint64s, i64)
		}
	}
	return &uint64s
}

func Uint64sToStrs(uint64s *[]uint64) *[]string {
	strs := make([]string, 0)
	for _, u64 := range *uint64s {
		strs = append(strs, strconv.FormatUint(u64, 10))
	}
	return &strs
}

func StrToBoolDef(text string, def bool) bool {
	ret, err := strconv.ParseBool(text)
	if err != nil {
		return def
	}
	return ret
}

func StrToIntDef(text string, def int) int {
	ret, err := strconv.Atoi(text)
	if err != nil {
		return def
	}
	return ret
}

func StrToUint64Def(text string, def uint64) uint64 {
	ret, err := strconv.ParseUint(text, 10, 64)
	if err != nil {
		return def
	}
	return ret
}

func StrToUint64s(text string, seq string) *[]uint64 {
	strs := strings.Split(text, seq)
	return StrsToUint64s(&strs)
}

func StrToInt64Def(text string, def int64) int64 {
	ret, err := strconv.ParseInt(text, 10, 64)
	if err != nil {
		return def
	}
	return ret
}

func SubString(str string, start, end int) string {
	runes := []rune(str)
	length := len(runes)

	if start < 0 || end > length || start > end {
		return ""
	}

	return string(runes[start:end])
}

func IsStrEmpty(str string) bool {
	return strings.TrimSpace(str) == ""
}
func IsStrNotEmpty(str string) bool {
	return !IsStrEmpty(str)
}

// RightReplace 从右边开始替换字符串
func RightReplace(str, old, new string, n int) string {
	if n < 0 {
		n = 0
	}

	// 找到从右边数的第n个old的位置
	idx := -1
	for i := len(str) - 1; i >= 0 && n > 0; i-- {
		if str[i:] == old {
			idx = i
			n--
		}
	}

	// 如果没有找到匹配，返回原字符串
	if idx == -1 {
		return str
	}

	// 从idx位置开始替换
	return str[:idx] + new + str[idx+len(old):]
}

// 替换字符串，忽略大小写
func ReplaceStrAllInsensitive(str, old, holder string) string {
	// 将原始字符串和要替换的子字符串都转换为小写
	lowerS := strings.ToLower(str)
	lowerOld := strings.ToLower(old)

	// 构建最终结果字符串
	var finalResult strings.Builder
	for i := 0; i < len(lowerS); {
		if i+len(lowerOld) <= len(lowerS) && lowerS[i:i+len(lowerOld)] == lowerOld {
			// 如果找到匹配的部分，添加新的字符串
			finalResult.WriteString(holder)
			i += len(lowerOld)
		} else {
			// 否则添加原始字符串中的字符
			finalResult.WriteByte(str[i])
			i++
		}
	}

	return finalResult.String()
}

// 替换字符串，忽略大小写, 只匹配一次
func ReplaceStrInsensitive(str, old, holder string) string {
	// 将原始字符串和要替换的子字符串都转换为小写
	lowerS := strings.ToLower(str)
	lowerOld := strings.ToLower(old)

	// 构建最终结果字符串
	var finalResult strings.Builder
	for i := 0; i < len(lowerS); {
		if i+len(lowerOld) <= len(lowerS) && lowerS[i:i+len(lowerOld)] == lowerOld {
			// 如果找到匹配的部分，添加新的字符串
			finalResult.WriteString(holder)
			finalResult.WriteString(str[i+len(lowerOld):])
			break
		} else {
			// 否则添加原始字符串中的字符
			finalResult.WriteByte(str[i])
			i++
		}
	}

	return finalResult.String()
}
